package com.cuij.servicelmpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cuij.dao.orderBusiDao;
import com.cuij.dao.orderCustDao;
import com.cuij.dao.orderGoodsDao;
import com.cuij.dao.orderInfoDao;
import com.cuij.entity.Result;
import com.cuij.entity.orderBusi;
import com.cuij.entity.orderCust;
import com.cuij.entity.orderGoods;
import com.cuij.entity.orderInfo;
import com.cuij.service.orderInfoService;
import com.cuij.util.DateUtil;
import com.cuij.util.UUIDUtil;

@Service
public class orderInfoServicelmpl implements orderInfoService {

    @Resource
    private orderInfoDao orderinfodao;
    @Resource
    private orderBusiDao orderbusidao;
    @Resource
    private orderCustDao ordercustdao;
    @Resource
    private orderGoodsDao ordergoodsdao;

    public Result createOrder(String goodsinfo, double totalprice, String busi_id, String use_id) {
        Result result = new Result();
        orderInfo orderinfo = new orderInfo();
        // 查商家信息
        Map busiInfo = orderbusidao.findBusiInfoByBusiId(busi_id);
        // 查客户信息
        Map custInfo = ordercustdao.findcustinfobyuseid(use_id);
        // 生成order_id
        String order_id = UUIDUtil.creatUUID().substring(0, 20);
        // 生成开始时间
        String order_start_time = DateUtil.getCurrentTime();
        // 信息入库
        orderinfo.setOrder_id(order_id);
        orderinfo.setCust_id(custInfo.get("CUST_ID").toString());
        orderinfo.setBusi_id(busi_id);
        orderinfo.setBusi_name(busiInfo.get("BUSI_NAME").toString());
        orderinfo.setBusi_linkname(busiInfo.get("BUSI_LINKNAME").toString());
        orderinfo.setBusi_linkphone(busiInfo.get("BUSI_LINKPHONE").toString());
        orderinfo.setBusi_address(busiInfo.get("BUSI_ADDRESS").toString());
        orderinfo.setGoods_info(goodsinfo);
        orderinfo.setTotal_price(totalprice);
        orderinfo.setOrder_start_time(order_start_time);
        orderinfo.setOrder_state("0");
        orderinfodao.createorder(orderinfo);

        result.setStatus(0);
        result.setData(order_id);
        return result;
    }

    // 查询订单信息
    public Result findOrderInfo(String order_id, String use_id) {
        Result result = new Result();
        Map order = orderinfodao.findorderinfo(order_id);
        // 查客户信息
        Map cust = ordercustdao.findcustinfobyuseid(use_id);
        List custList = new ArrayList();
        for (int i = 1; i <= 5; i++) {
            if (null != cust.get("CUST_ADDRESS" + i) && "" != cust.get("CUST_ADDRESS" + i).toString()) {
                custList.add(cust.get("CUST_ADDRESS" + i));
            }
        }
        String goodsInfo = order.get("GOODS_INFO").toString();
        String totalprice = order.get("TOTAL_PRICE").toString();
        List<String[]> orderInfoList = new ArrayList();
        String[] orderStr = goodsInfo.split("\\|");
        for (int i = 0; i < orderStr.length; i++) {
            String[] orderInfo = orderStr[i].split(",");
            orderInfoList.add(orderInfo);
        }
        int shopcount = orderStr.length;
        Map rspMap = new HashMap();
        rspMap.put("orderInfoList", orderInfoList);
        rspMap.put("shopcount", shopcount);
        rspMap.put("totalprice", totalprice);
        rspMap.put("custList", custList);
        result.setStatus(0);
        result.setData(rspMap);
        return result;
    }

    public Result confirmOrder(String use_id, String order_id, String address, double totalprice) {
        Result result = new Result();
        // 拆分字符串
        String str[] = address.split(",");
        String cust_name = str[0];
        String cust_phone = str[1];
        String cust_address = str[2];
        orderInfo orderinfo = new orderInfo();
        orderinfo.setCust_name(cust_name);
        orderinfo.setCust_phone(cust_phone);
        orderinfo.setCust_address(cust_address);
        orderinfo.setOrder_state("1");
        orderinfo.setOrder_id(order_id);
        orderinfodao.confirmorder(orderinfo);
        // 对用户金额查询
        Map custInfo = ordercustdao.findcustinfobyuseid(use_id);
        double cust_money = (Double) custInfo.get("CUST_MONEY");
        // 对客户扣款
        double custmoney = cust_money - totalprice;
        orderCust ordercust = new orderCust();
        ordercust.setUse_id(use_id);
        ordercust.setCust_money(custmoney);
        ordercustdao.updatecustmoney(ordercust);
        // 对商家查询
        Map orderInfo = orderinfodao.findorderinfo(order_id);
        String busi_id = (String) orderInfo.get("BUSI_ID");
        Map busiInfo = orderbusidao.findBusiInfoByBusiId(busi_id);
        // 对客户扣款
        double busimoney = (Double) busiInfo.get("BUSI_MONEY");
        double busi_money = busimoney + totalprice;
        orderBusi orderbusi = new orderBusi();
        orderbusi.setBusi_money(busi_money);
        orderbusi.setBusi_id(busi_id);
        orderbusidao.updatebusimoney(orderbusi);
        result.setStatus(0);
        return result;
    }

    // 查询所有订单信息
    public Result findOrderInfo(String order_id) {
        Result result = new Result();
        Map orderInfos = orderinfodao.findorderinfo(order_id);
        Map rspMap = new HashMap();
        // 拆分订单信息
        String goodsInfo = orderInfos.get("GOODS_INFO").toString();
        List<String[]> orderInfoList = new ArrayList();
        String[] orderStr = goodsInfo.split("\\|");
        for (int i = 0; i < orderStr.length; i++) {
            String[] orderInfo = orderStr[i].split(",");
            orderInfoList.add(orderInfo);
        }
        rspMap.put("orderInfoList", orderInfoList);
        rspMap.put("orderInfos", orderInfos);
        result.setStatus(0);
        result.setData(rspMap);
        return result;
    }

    // 显示订单
    public Result showOrder(String cust_id, String busi_id, String checkstate) {
        Result result = new Result();
        Map resMap = new HashMap();
        resMap.put("cust_id", cust_id);
        resMap.put("busi_id", busi_id);
        resMap.put("checkstate", checkstate);
        System.out.println("cust_id," + cust_id + "busi_id," + busi_id + "checkstate," + checkstate);
        List<Map<String, Object>> rspMap = orderinfodao.showorder(resMap);
        result.setData(rspMap);
        result.setStatus(0);
        return result;
    }

    public Result changeState(String order_id, String order_state) {
        Result result = new Result();
        orderInfo orderinfo = new orderInfo();
        if (order_state != null && order_state.equals("1")) {
            orderinfo.setOrder_state("2");
            orderinfo.setOrder_id(order_id);
            orderinfodao.changestate(orderinfo);
        }
        if (order_state != null && order_state.equals("2")) {
            orderinfo.setOrder_state("3");
            orderinfo.setOrder_id(order_id);
            orderinfodao.changestate(orderinfo);
        }
        if (order_state != null && order_state.equals("3")) {
            // 获取当前时间
            String endtime = DateUtil.getCurrentTime();
            Map orderInfos = orderinfodao.findorderinfo(order_id);
            String starttime = orderInfos.get("ORDER_START_TIME").toString();
            int betweenTime = DateUtil.betweenTime(starttime, endtime);
            String totaltime = "" + betweenTime;

            orderinfo.setOrder_state("4");
            orderinfo.setOrder_id(order_id);
            orderinfo.setOrder_end_time(endtime);
            orderinfo.setOrder_total_time(totaltime);
            orderinfodao.changestate(orderinfo);
            // 获取商品列表并获得商品的ID并修改商品销量
            String s = orderInfos.get("GOODS_INFO").toString();
            String[] goodslist = s.split("\\|");
            for (int i = 0; i < goodslist.length; i++) {
                String goods_id = goodslist[i].substring(0, 20);
                int count = ordergoodsdao.querysinglecount(goods_id);
                int newcount = count + 1;
                orderGoods ordergoods = new orderGoods();
                ordergoods.setGoods_count(newcount);
                ordergoods.setGoods_id(goods_id);
                ordergoodsdao.modifysinglecount(ordergoods);
            }
            // 获取销量综合
            String busi_id = orderInfos.get("BUSI_ID").toString();
            int allcount = ordergoodsdao.getallcount(busi_id);
            // 获取平均时间
            String s1 = orderinfodao.getavgtime(busi_id);
            String avgtime = s1.substring(0, s1.lastIndexOf("."));
            // 修改商家表
            orderBusi orderbusi = new orderBusi();
            orderbusi.setBusi_count(allcount);
            orderbusi.setBusi_speed(avgtime);
            orderbusi.setBusi_id(busi_id);
            orderbusidao.setcountandspeed(orderbusi);
        }
        return result;
    }

    // 删除超时订单
    public Result outTime(String cust_id) {
        Result result = new Result();
        List<orderInfo> rspMap = orderinfodao.querystarttime(cust_id);
        int length = rspMap.size();
        for (int i = 0; i < length; i++) {
            orderInfo orderinfo = rspMap.get(i);
            String starttime = orderinfo.getOrder_start_time();
            String order_id = orderinfo.getOrder_id();
            String nowtime = DateUtil.getCurrentTime();
            int between = DateUtil.betweenTime(starttime, nowtime);
            if (between > 30) {
                orderinfodao.outtime(order_id);
            }
        }
        return result;
    }

    // 评论成功后操作
    public Result commentSuccess(String order_id, double busi_level, double goods_level, String busi_comment) {
        Result result = new Result();
        // 先修改订单状态
        orderInfo orderinfo = new orderInfo();
        orderinfo.setOrder_id(order_id);
        orderinfo.setOrder_state("5");
        orderinfo.setBusi_level(busi_level);
        orderinfo.setGoods_level(goods_level);
        orderinfo.setBusi_comment(busi_comment);
        orderinfodao.setcomment(orderinfo);
        // 修改单个产品评分
        Map orderInfos = orderinfodao.findorderinfo(order_id);
        String s = orderInfos.get("GOODS_INFO").toString();
        String[] goodslist = s.split("\\|");
        for (int i = 0; i < goodslist.length; i++) {
            String goods_id = goodslist[i].substring(0, 20);
            orderGoods ordergoods = new orderGoods();
            int count = ordergoodsdao.querysinglecount(goods_id);
            double level = ordergoodsdao.querysinglelevel(goods_id);
            if (count == 0) {
                ordergoods.setGoods_level(level);
                ordergoods.setGoods_id(goods_id);
            }
            else {
                int newcount = count - 1;
                double newlevel = ((newcount * level) + goods_level) / count;
                ordergoods.setGoods_level(newlevel);
                ordergoods.setGoods_id(goods_id);
            }
            ordergoodsdao.modifysinglelevel(ordergoods);
        }
        // 修改商家评分
        String busi_id = orderInfos.get("BUSI_ID").toString();
        double avglevel = orderinfodao.getavglevel(busi_id);
        orderBusi orderbusi = new orderBusi();
        orderbusi.setBusi_id(busi_id);
        orderbusi.setBusi_level(avglevel);
        orderbusidao.setlevel(orderbusi);
        return result;
    }
}
